<template>
  <div class="dashboard-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="类别" prop="type">
        <el-select v-model="form.type" placeholder="请选择类别">
          <el-option label="分类" value="1" />
          <el-option label="规章制度" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.type === '2'" label="分类" prop="sid">
        <el-select v-model="form.sid" placeholder="请选择分类">
          <el-option v-for="item in classList" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.type === '2'" label="内容" prop="content">
        <tinymce v-model="form.content" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">提交</el-button>
        <!-- <el-button @click="resetForm('form')">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import { uploadRulesApi, getRulesClassApi, getRulesInfoApi } from '@/api/main'
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      classList: [],
      form: {
        title: '',
        type: '',
        sid: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类别', trigger: 'change' }
        ],
        sid: [
          { required: true, message: '请选择分类', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRulesType()
    this.getRulesInfo()
  },
  methods: {
    getRulesType() {
      getRulesClassApi().then(res => {
        this.classList = res.data
      })
    },
    getRulesInfo() {
      const data = {
        id: this.$route.params.id
      }
      getRulesInfoApi(data).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.form = res.data
        }
      })
    },
    submitForm(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // console.log(this.form)
          const data = new FormData()
          data.append('id', this.form.id)
          data.append('title', this.form.title)
          data.append('content', JSON.stringify(this.form.content))
          data.append('type', this.form.type)
          data.append('sid', this.form.sid)
          console.log(data)
          uploadRulesApi(data).then(res => {
            console.log(res)
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.form = {
                id: '',
                title: '',
                type: '',
                sid: '',
                content: ''
              }
              this.$router.push({
                name: 'rules-list'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
    &-container {
        margin: 30px;
    }
    &-text {
        font-size: 30px;
        line-height: 46px;
    }
}
</style>
